/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include <new>
#include <assert.h>

#include "gametypes.h"
#include "interfaces.h"
#include "globjects.h"
#include "mainoptions.h"

using namespace std;

//-----------------------
// AngelScript functions
//-----------------------

static void CVector3EmptyCnstr(CVector3 *self)
{
	new(self) CVector3();
}

static void CVector3CopyCnstr(const CVector3 &other, CVector3 *self)
{
	new(self) CVector3(other);
}

static void CVector3Cnstr(float x, float y, float z, CVector3 *self)
{
	new(self) CVector3(x,y,z);
}

static void CMaterialEmptyCnstr(CMaterial *self)
{
	// The pointer is managed script side. The scripting engine allocates a memory pool
	// large enough to hold an instance of CMaterial, but does not call its contructor.
	// self is a pointer to a pre-allocated memory, hence the new called here is the
	// new "placement" operator. Only the contructor is called, no memory is allocated here.
	new(self) CMaterial();
}

static void CMaterialCopyCnstr(const CMaterial &other, CMaterial *self)
{
	// same as above, but calls the copy constructor instead of the empty one.
	new(self) CMaterial(other);
}

static void CMaterialDestr(CMaterial *self)
{
	// The (pointer) memory pool is managed script side, but the destructor needs to
	// be called code-side (same as the constructor, see above)
	self->~CMaterial();
}


// ----------------------

void RegisterVector(asIScriptEngine* engine, std::map<string, int> &typeIDs)
{
	int r;

	// Register the type
	r = engine->RegisterObjectType("Vector3", sizeof(CVector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK); assert( r >= 0 );

	// Register the constructors
	r = engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT,  "void f()", asFUNCTION(CVector3EmptyCnstr), asCALL_CDECL_OBJLAST); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT,  "void f(const Vector3 &in)", asFUNCTION(CVector3CopyCnstr), asCALL_CDECL_OBJLAST); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT,  "void f(float, float y = 0, float z = 0)",  asFUNCTION(CVector3Cnstr), asCALL_CDECL_OBJLAST); assert( r >= 0 );

	// Register the operator overloads
	r = engine->RegisterObjectMethod("Vector3", "Vector3 &opAssign(Vector3 &in)", asMETHODPR(CVector3, operator=, (CVector3), CVector3&), asCALL_THISCALL); assert( r >= 0 );

	// Register the object methods
	r = engine->RegisterObjectMethod("Vector3", "float length() const", asMETHOD(CVector3,GetLength), asCALL_THISCALL); assert( r >= 0 );

	r = engine->RegisterObjectMethod("Vector3", "void set_xyz(float x, float y, float z)", asMETHOD(CVector3, SetComponents), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Vector3", "float x()", asMETHOD(CVector3, x), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Vector3", "float y()", asMETHOD(CVector3, y), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Vector3", "float z()", asMETHOD(CVector3, z), asCALL_THISCALL); assert( r >= 0 );

    typeIDs["Vector3"] = engine->GetTypeIdByDecl("Vector3");
}

void RegisterEnums(asIScriptEngine* engine)
{
	int r;

	// Register enums
	r = engine->RegisterEnum("eObjectType"); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectType", "TYPE_MOVABLE", 101); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectType", "TYPE_STATIC", 102); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectType", "TYPE_PROP", 103); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectType", "TYPE_LIGHT", 104); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectType", "TYPE_CAMERA", 105); assert(r >= 0);

	r = engine->RegisterEnum("eObjectID"); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_CUE_BALL", 0); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_1", 1); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_2", 2); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_3", 3); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_4", 4); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_5", 5); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_6", 6); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_7", 7); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_8", 8); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_9", 9); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_10", 10); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_11", 11); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_12", 12); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_13", 13); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_14", 14); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_BALL_15", 15); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_1", 16); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_2", 17); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_3", 18); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_4", 19); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_5", 20); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_SOCKET_6", 21); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_GROUND", 22); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_TABLE", 23); assert(r >= 0);
	r = engine->RegisterEnumValue("eObjectID", "OBJ_GENERIC", 24); assert(r >= 0);

	r = engine->RegisterEnum("eCollisionShape"); assert(r >= 0);
	r = engine->RegisterEnumValue("eCollisionShape", "CS_PLANE", 201); assert(r >= 0);
	r = engine->RegisterEnumValue("eCollisionShape", "CS_SPHERE", 202); assert(r >= 0);
	r = engine->RegisterEnumValue("eCollisionShape", "CS_SOCKET", 203); assert(r >= 0);
	r = engine->RegisterEnumValue("eCollisionShape", "CS_BOX", 204); assert(r >= 0);

	r = engine->RegisterEnum("eLightType"); assert(r >= 0);
	r = engine->RegisterEnumValue("eLightType", "LIGHT_POINT", 301); assert(r >= 0);
	r = engine->RegisterEnumValue("eLightType", "LIGHT_SPOT", 302); assert(r >= 0);

	r = engine->RegisterEnum("eEventType"); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_KEY_PRESS", 1001); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_CAMERA_MOVED", 1002); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_CUEBALL_LAUNCHED", 1003); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_BALL_MOVED", 1004); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_BALL_COLLISION", 1005); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_TABLE_COLLISION", 1006); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_BALL_POCKETED", 1007); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_BALL_ILLEGALLY_POCKETED", 1008); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_CUEBALL_POCKETED", 1009); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_START_GAME_REQUESTED", 1010); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_GAME_ENDED", 1011); assert(r >= 0);
	r = engine->RegisterEnumValue("eEventType", "EV_QUIT_APPLICATION", 1012); assert(r >= 0);
}

void RegisterInterfaces(asIScriptEngine* engine, std::map<string, int> &typeIDs)
{
	int r;
	// register needed game interfaces

	// Event and EventListener
	r = engine->RegisterInterface("IEvent"); assert(r >= 0);
	r = engine->RegisterInterfaceMethod("IEvent", "eEventType getEventType()"); assert(r >= 0);
	r = engine->RegisterInterfaceMethod("IEvent", "string getEventTypeStr()"); assert(r >= 0);

	r = engine->RegisterInterface("IEventListener"); assert(r >= 0);
	r = engine->RegisterInterfaceMethod("IEventListener", "string getName()"); assert(r >= 0);
	// might be a problem:
	r = engine->RegisterInterfaceMethod("IEventListener", "bool processEvent(IEvent @event)"); assert(r >= 0);

	typeIDs["IEvent"] = engine->GetTypeIdByDecl("IEvent");
	typeIDs["IEventListener"] = engine->GetTypeIdByDecl("IEventListener");
}

void RegisterObjects(asIScriptEngine* engine, std::map<string, int> &typeIDs)
{
	int r;

	// CMaterial
	// Register the type
	r = engine->RegisterObjectType("Material", sizeof(CMaterial), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );

	// Register the constructors
	r = engine->RegisterObjectBehaviour("Material", asBEHAVE_CONSTRUCT,  "void f()", asFUNCTION(CMaterialEmptyCnstr), asCALL_CDECL_OBJLAST); assert( r >= 0 );
	// copy constructor
	r = engine->RegisterObjectBehaviour("Material", asBEHAVE_CONSTRUCT,  "void f(const Material &in)", asFUNCTION(CMaterialCopyCnstr), asCALL_CDECL_OBJLAST); assert( r >= 0 );
	//deconstructor
	r = engine->RegisterObjectBehaviour("Material", asBEHAVE_DESTRUCT,  "void f()", asFUNCTION(CMaterialDestr), asCALL_CDECL_OBJLAST); assert( r >= 0 );

	// Register the operator overloads
	r = engine->RegisterObjectMethod("Material", "Material &opAssign(Material &in)", asMETHODPR(CMaterial, operator=, (CMaterial&), CMaterial&), asCALL_THISCALL); assert( r >= 0 );

	// Register the object properties
	r = engine->RegisterObjectMethod("Material", "void set_color(int b, int g, int b, int a) const", asMETHOD(CMaterial, SetColor), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Material", "void set_ambient(float r, float g, float b, float a)", asMETHOD(CMaterial, SetAmbient), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Material", "void set_diffuse(float r, float g, float b, float a) const", asMETHOD(CMaterial, SetDiffuse), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Material", "void set_specular(float r, float g, float b, float a) const", asMETHOD(CMaterial, SetSpecular), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Material", "void set_shininess(float s) const", asMETHOD(CMaterial, SetShininess), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("Material", "void set_shader(string &in) const", asMETHOD(CMaterial, SetGPUProgram), asCALL_THISCALL); assert( r >= 0 );

	typeIDs["Material"] = engine->GetTypeIdByDecl("Material");

	// CLevelLoader
	r = engine->RegisterObjectType("LevelLoader", 0, asOBJ_REF); assert( r >= 0 );

	r = engine->RegisterObjectBehaviour("LevelLoader", asBEHAVE_ADDREF, "void f()", asMETHOD(CLevelLoader, AddRef), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour("LevelLoader", asBEHAVE_RELEASE, "void f()", asMETHOD(CLevelLoader, Release), asCALL_THISCALL); assert( r >= 0 );

	r = engine->RegisterObjectMethod("LevelLoader", "void new_object(dictionary &in)", asMETHOD(CLevelLoader, NewObject), asCALL_THISCALL); assert( r >= 0 );

	typeIDs["LevelLoader"] = engine->GetTypeIdByDecl("LevelLoader");
}

void RegisterGameTypes(asIScriptEngine* engine, std::map<string, int> &typeIDs)
{
	RegisterEnums(engine);
	RegisterInterfaces(engine, typeIDs);
	RegisterObjects(engine, typeIDs);
}
